package com.lmc.orderservice.service;

import com.lmc.orderservice.mapper.OrdreMapper;
import com.lmc.orderservice.model.OrderInfo;
import com.lmc.orderservice.model.ProductInfo;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.service.annotation.PostExchange;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class OrderService {

    @Autowired
    private OrdreMapper ordreMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
//  请求计数器
    private AtomicInteger count = new AtomicInteger(1);

    private List<ServiceInstance> instances;

//    初始化实例
/**
 * @PostConstruct 注解用于在依赖注入完成后执行初始化操作
 * 该方法会在Spring容器初始化当前Bean时自动调用
 * 此处用于初始化获取product-service的服务实例列表
 */
    @PostConstruct
    public void init(){
        instances = discoveryClient.getInstances("product-service");

    }

//    public OrderInfo selectOrderById(Integer orderId) {
//        OrderInfo orderInfo = ordreMapper.selectOrderById(orderId);
////        String url = "http://127.0.0.1:8090/product/getProductInfo?productId=" + orderInfo.getProductId();
//        List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
////        遍历这个列表
////        for (ServiceInstance instance : instances) {
////            log.info("instance:{}", instance);
////        }
////        log.info("instance size:{}", instances.size());
//        String uri = instances.get(0).getUri().toString() ;
//        String url = uri + "/product/getProductInfo?productId=" + orderInfo.getProductId();
//        log.info("远程调用url:{}", url);
//        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//        orderInfo.setProductInfo(productInfo);
//        return orderInfo;
//    }


//public OrderInfo selectOrderById(Integer orderId) {
//    OrderInfo orderInfo = ordreMapper.selectOrderById(orderId);
////        String url = "http://127.0.0.1:8090/product/getProductInfo?productId=" + orderInfo.getProductId();
//    int index = count.getAndIncrement() % instances.size();
//    String uri = instances.get(index).getUri().toString() ;
//    String url = uri + "/product/getProductInfo?productId=" + orderInfo.getProductId();
//    log.info("远程调用url:{}", url);
//    ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//    orderInfo.setProductInfo(productInfo);
//    return orderInfo;
//}

public OrderInfo selectOrderById(Integer orderId) {
    OrderInfo orderInfo = ordreMapper.selectOrderById(orderId);
    String url = "http://product-service/product/getProductInfo?productId=" + orderInfo.getProductId();
    log.info("远程调用url:{}", url);
    ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
    orderInfo.setProductInfo(productInfo);
    return orderInfo;
}

}
